import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'
import { getToken } from '@/utils/auth'

Vue.use(Router)

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '主页', icon: 'el-icon-crop', requiresAuth: true }
    }]
  },

  {
    path: '/search',
    component: () => import('@/views/search/index'),
    name: 'Search',
    meta: { title: '菜品搜索' }
  },

  {
    path: '/dish',
    component: Layout,
    redirect: '/dish/list',
    name: 'Dish',
    meta: { title: '菜品浏览', icon: 'el-icon-dish' },
    children: [
      {
        path: 'list',
        name: 'DishList',
        component: () => import('@/views/dish/DishList.vue'),
        meta: { title: '菜品列表', icon: 'el-icon-menu' }
      },
      {
        path: 'detail',
        name: 'DishDetail',
        component: () => import('@/views/dish/DishDetail.vue'),
        hidden: true,
        meta: { title: '菜品详情', activeMenu: '/dish/list' }
      }
    ]
  },

  {
    path: '/store',
    component: Layout,
    redirect: '/store/list',
    name: 'Store',
    meta: { title: '商店浏览', icon: 'el-icon-shop' },
    children: [
      {
        path: 'list',
        name: 'StoreList',
        component: () => import('@/views/store/StoreList.vue'),
        meta: { title: '商店列表', icon: 'el-icon-menu' }
      },
      {
        path: 'detail/:id',
        name: 'StoreDetail',
        component: () => import('@/views/store/StoreDetail.vue'),
        hidden: true,
        meta: { title: '商店详情', activeMenu: '/store/list' }
      }
    ]
  },

  {
    path: '/cart',
    component: () => import('@/views/cart/index.vue'),
    name: 'Cart',
    meta: { 
      title: '购物车',
      requiresAuth: true
    }
  },

  {
    path: '/payment',
    component: () => import('@/views/payment/index.vue'),
    name: 'Payment',
    meta: { 
      title: '支付页面',
      requiresAuth: true
    }
  },

  {
    path: '/store-management',
    component: Layout,
    redirect: '/store-management/dishes',
    name: 'StoreManagement',
    meta: { title: '商店管理', icon: 'el-icon-setting', requiresAuth: true },
    children: [
      {
        path: 'dishes',
        name: 'DishManagement',
        component: () => import('@/views/store-management/DishManagement.vue'),
        meta: { title: '菜品管理', icon: 'el-icon-dish' }
      },
      {
        path: 'info',
        name: 'StoreInfo',
        component: () => import('@/views/store-management/StoreInfo.vue'),
        meta: { title: '商店信息', icon: 'el-icon-info' }
      }
    ]
  },

  {
    path: '/information',
    component: () => import('@/views/information/index.vue'),
    name: 'Information',
    meta: {
      title: '个人中心',
      requiresAuth: true
    }
  },

  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
